package com.lagou.framework.coyote;

import com.lagou.framework.common.HttpServlet;
import com.lagou.framework.config.XmlServerConfig;
import com.lagou.framework.util.ThreadPoolUtil;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Map;

/**
 * 负责socket相关操作，接收到请求后调用process
 *
 * @author srh
 * @date 2020/04/02
 **/
public class EndPoint {

    private static EndPoint instance = new EndPoint();

    public static EndPoint getInstance() {
        return instance;
    }

    private Integer port;

    public void init(XmlServerConfig.Connector connector) {
        Integer configPort = connector.getPort();
        this.port = (null == configPort) ? 8080 : configPort;
    }

    public void start() throws IOException {
        ServerSocket serverSocket = new ServerSocket(port);
        System.out.println("Minicat startup port: " + port);

        while (true) {
            Socket socket = serverSocket.accept();
            Processor processor = new Processor(socket);
            ThreadPoolUtil.getExecutor().execute(processor);
        }
    }
}
